#ifndef __UART_CONFIG_H__
#define __UART_CONFIG_H__

#include <stdint.h>
#include <stdbool.h>
#include "usart.h"

#define UART_CONFIG_ADDRESS  0x00082000UL
#define UART_CONFIG_COUNT    6             // 支持 6 组配置

//串口波特率14种
typedef enum
{
    BAUD_600 = 600,
    BAUD_1200 = 1200,
    BAUD_2400 = 2400,
    BAUD_4800 = 4800,
    BAUD_9600 = 9600,
    BAUD_14400 =14400,
    BAUD_19200 =19200,
    BAUD_38400 =38400,
    BAUD_57600 =57600,
    BAUD_115200 = 115200,
    BAUD_230400 = 230400,
    BAUD_345600 = 345600,
    BAUD_460800 = 460800,
    BAUD_921600 = 921600,
} UartBaudRateEnum;


typedef enum {
    DATA_BITS_8 = 8,
    DATA_BITS_9 = 9
} UartDataBitsEnum;
static inline uint32_t GetWordLengthValue(UartDataBitsEnum eBits)
{
    switch(eBits)
    {
        case DATA_BITS_8: return UART_WORDLENGTH_8B;
        case DATA_BITS_9: return UART_WORDLENGTH_9B;
        default:          return UART_WORDLENGTH_8B;
    }
}


typedef enum {
    STOP_BITS_1 = 1,
    STOP_BITS_2 = 2
} UartStopBitsEnum;
static inline uint32_t GetStopBitsValue(UartStopBitsEnum eStop)
{
    switch(eStop)
    {
        case STOP_BITS_1: return UART_STOPBITS_1;
        case STOP_BITS_2: return UART_STOPBITS_2;
        default:          return UART_STOPBITS_1;
    }
}

typedef enum {
    PARITY_NONE = 0,
    PARITY_EVEN = 1,
    PARITY_ODD  = 2
} UartParityEnum;

static inline uint32_t GetParityValue(UartParityEnum eParity)
{
    switch(eParity)
    {
        case PARITY_NONE: return UART_PARITY_NONE;
        case PARITY_EVEN: return UART_PARITY_EVEN;
        case PARITY_ODD:  return UART_PARITY_ODD;
        default:          return UART_PARITY_NONE;
    }
}


// 配置结构体
typedef struct {
    UartBaudRateEnum BaudRate;
    UartDataBitsEnum DataBits;
    UartStopBitsEnum StopBits;
    UartParityEnum   Parity;
} UartConfig_TypeDef;

 //声明外部全局socket配置项
extern UartConfig_TypeDef local_uart_config[UART_CONFIG_COUNT];

void LoadUartConfigFromFlash(void);
void SaveUartConfigToFlash(void);
void ResetUartConfigToDefault(void);

#endif
